<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>批量设置次日库存</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/default/easyui.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/icon.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/demo.css"/>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.easyui.min.js"></script>
	<script type="text/javascript">
		$(function(){
			loadTable();
			$('#dlg').dialog('close');
			
		});
		
		function loadTable(){
			$('#dg').datagrid({
				rownumbers:true,
				singleSelect:true,
				pagination:true,
				pageSize:500,
				pageList:[500,1000],
		 		url:'${pageContext.request.contextPath }/goods/fixStoreList',
	    		columns:[[
					{field:'goods_id',title:'商品编号',align:'center',width:80},
					{field:'set_date',title:'库存日期',align:'center',width:80},
					{field:'goods_name',title:'商品名称',align:'center',width:200},
					{field:'app_name',title:'APP名称',align:'center',width:100},
					{field:'goods_kindname',title:'商品类别',align:'center',width:100},
					{field:'store_num',title:'当前预估库存',align:'center',width:80},
					{field:'sale_num',title:'当日已售数量',align:'center',width:80},
					{field:'actual_store',title:'目前库存(件)',align:'center',width:80,formatter: function (value,row,index){
					    var actual_store=row.store_num-row.sale_num;
					    return actual_store;
					}},
					{field:'store_num0',title:'设置次日预估库存',width:100,align:'center',width:100,formatter: function (value,row,index){
					    var slname="store"+index;
					    return '<input type="text" id='+slname+' size="8" maxlength="8"  style="text-align:right;" name="sale" value='+value+' />';
					}},
					{field:'limit_num0',title:'设置次日限购数量',width:100,align:'center',width:100,formatter: function (value,row,index) {
					    var slunit="limit"+index;
						return '<input type="text" id='+slunit+' size="8" maxlength="8"  style="text-align:right;" name="unit" value='+value+' />';
					}}
	 			]],
	 			onLoadSuccess:function(data){
			       //添加“合计”列
		           calcTotal();		       
				},
				toolbar: [{
						text:'库存确认',
						iconCls:'icon-save',
						handler:function(){
							//show("${pageContext.request.contextPath}/goods/setgoods","定价确认");
							save();
						}
					}]
			});
		}
		
		function calcTotal(){
			var rows = $('#dg').datagrid('getRows');
            var totalStore = 0;
            var totalSale = 0;
            var currentStore=0;
            for (var i = 0; i < rows.length; i++) {
                totalStore += parseInt(rows[i]['store_num']);
                totalSale += parseInt(rows[i]['sale_num']);
                var current=parseInt(rows[i]['store_num'])-parseInt(rows[i]['sale_num']);
                currentStore+=current;
            }
            $("#totalStore").html(totalStore+" 件");  
            $("#totalSale").html(totalSale+" 件");
            $("#currentStore").html(currentStore+" 件");
		}
		
		function putaway(goods_id){
            $.post("${pageContext.request.contextPath}/goods/putAway",{goods_id:goods_id,goods_status:2},
				function(data){
					if(data=="0"){
						$.messager.alert('系统消息','<span style=\"color:red;\">商品下架失败，请稍后再试！</span>');	 
					}else{
						$.messager.alert('系统消息','商品下架成功！');
						$('#dg').datagrid('reload'); 
					}
				},"text");
        }
		
		function showImg(imgUrl){
			$('#dlg').dialog('open');
			var image=document.getElementById("stationImg");
			var src="${pageContext.request.contextPath }"+imgUrl;
			image.setAttribute("src",src );
		}
		
		function edit(channelId){
			show("${pageContext.request.contextPath}/admin/channel/editBefore/"+channelId,"修改栏目信息");
			
		}

		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name){
			parent.window.addTab(name,url);
		}
		
		function save(){
			$.messager.defaults = { ok: "是", cancel: "否" };
			$.messager.confirm('系统确认',"请您仔细核对库存信息，您确定要修改库存吗？<br/><span style='color:red;'>[温馨提示]：一旦修改，将影响到客户下单！</span>", function(r){
				if (r){
					var rows = $("#dg").datagrid("getRows");
					  var postdata = new Array();
					  for(i in rows)
					  {
					      var goods_id = rows[i]["goods_id"];
						  var slstore = $('#store'+i).val();
						  var slimit = $('#limit'+i).val();
						  //alert("value:"+goods_id+"  "+slsale+"  "+slunit);
						  
						  var para = new Object();
			              para.goods_id= goods_id;
						  para.slstore= slstore;
						  para.slimit= slimit;
						  postdata.push(para);
					  }
					  //alert($.toJSON(postdata));
					  var para = JSON.stringify(postdata);
					  
					  $.ajax({
				    	url:"${pageContext.request.contextPath}/goods/batchSaveStore",  
					    type:"post",
			            dataType : 'json',
					    data:para,
					    success:function(data){
					       window.location.reload();
					    },
					    error : function(msg) {
					        console.info(msg);
					    }
					});
				}else{
					$.messager.alert('系统消息','批量设置次日库存操作取消！');
				}
			}); 		  
		}
	</script>
	<style type="text/css">
		body{
		font-family:'微软雅黑';}
	</style>
  </head>
  
  <body>
    <table id="dg" title="批量设置次日库存" style="width:90%;"></table>
    <div style="width:100%; padding-left:520px;">
    	<div style="width:78px; float:left; text-align:center; font-weight:bold;">合计：</div> 	
    	<div style="width:78px; float:left; text-align:center; font-weight:bold;" id="totalStore">0 件</div>
    	<div style="width:78px; float:left; text-align:center; font-weight:bold;" id="totalSale">0 件</div>
    	<div style="width:78px; float:left; text-align:center; font-weight:bold;" id="currentStore">0 件</div>
    </div>
  </body>
</html>
